package cn.wuhan2019mwg.mobile.activity.wuhan2019mwg.activitys;

import android.content.Intent;
import android.os.Build;
import android.os.Handler;
import android.support.v7.widget.AppCompatTextView;
import android.util.Log;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import cn.wuhan2019mwg.mobile.activity.wuhan2019mwg.R;
import cn.wuhan2019mwg.mobile.activity.wuhan2019mwg.base.BaseActivity;
import cn.wuhan2019mwg.mobile.activity.wuhan2019mwg.utils.Constance;

public class WelcomeWebActivity extends BaseActivity implements View.OnClickListener{
    private String tag ="WelcomeWebActivity";
    private ImageView iv_go_back;
    private AppCompatTextView tv_webtitle;
    private ProgressBar pb_other_progress;
    private WebView wb_other;
    private String url="http://2019.haoyicn.cn/app/welcome.html";
    private RelativeLayout rl_go_back;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_welcome_web;
    }

    @Override
    protected void initView() {
        iv_go_back = (ImageView) findViewById(R.id.iv_go_back);
        tv_webtitle = (AppCompatTextView) findViewById(R.id.tv_webtitle);
        tv_webtitle.setSelected(true);
        pb_other_progress = (ProgressBar) findViewById(R.id.pb_other_progress);
        rl_go_back = (RelativeLayout) findViewById(R.id.rl_go_back);
        wb_other = (WebView) findViewById(R.id.wb_other);
        rl_go_back.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        setWebClient();
        handler.postDelayed(runnable, Constance.START_TIME1);
    }

    private Handler handler = new Handler();

    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            Intent intent = new Intent();
            intent.setClass(WelcomeWebActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case  R.id.rl_go_back:
                if (wb_other.canGoBack()){
                    wb_other.goBack();
                }else {
                    finish();
                }
                break;
        }
    }

    private void setWebClient(){
        wb_other.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        wb_other.getSettings().setJavaScriptEnabled(true);// 设置可用JS
        wb_other.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        wb_other.getSettings().setUseWideViewPort(true);
        wb_other.getSettings().setLoadWithOverviewMode(true);
        wb_other.getSettings().setSaveFormData(true);
        wb_other.getSettings().setAllowFileAccess(true);
        wb_other.getSettings().setGeolocationEnabled(true);
        wb_other.getSettings().setDomStorageEnabled(true);
        wb_other.getSettings().setBuiltInZoomControls(true);
        wb_other.getSettings().setSupportZoom(true); // 支持缩放
        wb_other.getSettings().setPluginState(WebSettings.PluginState.ON);
        wb_other.getSettings().setSupportMultipleWindows(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            wb_other.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        wb_other.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    //view.loadUrl(request.getUrl().toString());
                }
                wb_other.loadUrl("javascript:sayHello()");
                return false;
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return false;
            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
            }
        });

        wb_other.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    // 网页加载完成
                    pb_other_progress.setVisibility(View.GONE);
                } else {
                    // 加载中
                    pb_other_progress.setProgress(newProgress);
                }
                super.onProgressChanged(view, newProgress);
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                tv_webtitle.setText(title);
            }
        });
        wb_other.loadUrl(url);
    }

    @Override
    public void onBackPressed() {
        if (wb_other.canGoBack()){
            wb_other.goBack();
        }else {
            finish();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        wb_other.clearCache(true);
        wb_other.clearHistory();
        handler.removeCallbacks(runnable);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacks(runnable);
    }
}
